package com.zhoug.zmuidemo.ui;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.Toast;

import com.zhoug.common.utils.AppUtils;
import com.zhoug.common.utils.ResourceUtils;
import com.zhoug.zmui.popup.ZMUIBasePopupWindow;
import com.zhoug.zmui.popup.ZMUIListPopupWindow;
import com.zhoug.zmui.popup.ZMUISelectListPopupWindow;
import com.zhoug.zmui.round.ZMUIRoundButton;
import com.zhoug.zmuidemo.R;

import java.util.Arrays;

public class PopupActivity extends AppCompatActivity {
    private ZMUIRoundButton mBtn1;
    private ZMUIRoundButton mBtn2;
    private ZMUIRoundButton mBtn3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_popup);
        findViews();
    }

    public void showCustomPopup(View view) {
        ZMUIBasePopupWindow popupWindow = new ZMUIBasePopupWindow(this);
        View inflate = LayoutInflater.from(this).inflate(R.layout.popup_custom, null);
        popupWindow.setContentView(inflate);
        popupWindow.showAsDropDown(mBtn1);


    }


    private void findViews() {
        mBtn1 = findViewById(R.id.btn1);
        mBtn2 = findViewById(R.id.btn2);
        mBtn3 = findViewById(R.id.btn3);
    }

    public void showZMUIListPopupWindow(View view) {
        String[] strings = {
                "Item1", "Item2", "Item3", "Item4", "Item5",
                "Item6", "Item7", "Item8", "Item9", "Item10",
                "Item11", "Item12", "Item13", "Item14", "Item15",
        };
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list__item, strings);

        ZMUIListPopupWindow listPopupWindow = new ZMUIListPopupWindow(this);
        listPopupWindow.setListAdapter(adapter);
        listPopupWindow.showDivider(true);
        listPopupWindow.setWidth(view.getWidth());
        listPopupWindow.setOnItemClickListener(p -> {
            Toast.makeText(this, strings[p], Toast.LENGTH_SHORT).show();
        });
        listPopupWindow.setMaxHeight(600);
        listPopupWindow.setWidth(mBtn2.getWidth());
//        listPopupWindow.showAsDropDown(mBtn2);
        listPopupWindow.showAsDropDown(mBtn2, 0, 0, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL);
    }

    ZMUISelectListPopupWindow<String> popupWindow;

    public void showZMUISelectListDialog(View view) {

        if (popupWindow == null) {
            String[] strings = {
                    "Item1", "Item2", "Item3", "Item4", "Item5",
                    "Item6", "Item7", "Item8", "Item9", "Item10",
                    "Item11", "Item12", "Item13", "Item14", "Item15",
            };
             popupWindow = new ZMUISelectListPopupWindow<String>(this) {
                @Override
                public String getText(String itemData) {
                    return itemData;
                }
            };

            popupWindow.setSelected(3);
            popupWindow.setData(Arrays.asList(strings));
            popupWindow.setOnSelectedListener(pos -> {
                Toast.makeText(this, "" + popupWindow.getItemData(pos), Toast.LENGTH_SHORT).show();
            });
//            popupWindow.setDivider(new ColorDrawable(Color.RED));
            popupWindow.showDivider(true);
            ColorStateList colorStateList = ColorStateList.valueOf(Color.RED);
            popupWindow.setTint(colorStateList);
            popupWindow.setMaxHeight(600);
            popupWindow.setWidth(mBtn3.getWidth());
        }

        popupWindow.showAsDropDown(mBtn3);

    }
}